<template>
  <!-- 新增/编辑字典 -->
  <el-dialog :title="dialogTitle" v-model="dictParams.visible" @open="handleOpen" width="500" :close-on-click-modal="false">
    <div class="dict-main" v-loading="loading">
      <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="90">
        <el-form-item label="字典名称" prop="classifyName">
          <el-input v-model.trim="formData.classifyName" maxlength="60" clearable placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典编码" prop="classifyCode">
          <el-input v-model.trim="formData.classifyCode" maxlength="60" clearable placeholder="请输入角色编码" />
        </el-form-item>
        <el-form-item label="状态" prop="enable">
          <el-radio-group v-model="formData.enable">
            <el-radio v-for="(value, key, index) in STATUS_TYPE" :value="Number(key)" :key="index">{{ value }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model.trim="formData.description" maxlength="99" show-word-limit :autosize="{ minRows: 3, maxRows: 5 }"
            type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emit('close')">返回</el-button>
        <el-button type="primary" :disabled="loading" @click="handleSubmit">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, computed, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import { addDict, modifyDict } from '@views/dictManage/api/dictManage'
import { STATUS_TYPE } from '@constants/dictionary'
const props = defineProps({
  dictParams: {
    type: Object,
    default: () => { },
    required: true
  }
})

const emit = defineEmits(['close'])
const formData = reactive({ enable: 1 })
const ruleFormRef = ref(null)
const loading = ref(false)
const dialogTitle = computed(() => props.dictParams.row?.roleId ? '编辑字典' : '新增字典')
const rules = reactive({
  classifyName: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
  classifyCode: [{ required: true, message: '请输入字典编号', trigger: 'blur' }],
  enable: [{ required: true, message: '请选择状态', trigger: 'change' }],
  description: [{ required: true, message: '请输入描述', trigger: 'blur' }]
})

const handleSubmit = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      handleSave()
    } else {
      return false
    }
  })
}
const handleSave = async () => {
  loading.value = true
  const res = formData.id ? await modifyDict(formData) : await addDict(formData)
  loading.value = false
  if (res?.code === 200) {
    ElMessage.success('保存成功')
    emit('close', true)
  }
}
// 初始化弹窗
const handleOpen = () => {
  nextTick(() => {
    ruleFormRef.value.resetFields()

    let row = props.dictParams.row
    if (row.id) {
      // 编辑
      formData.id = row.id
      formData.classifyName = row.classifyName
      formData.classifyCode = row.classifyCode
      formData.enable = row.enable
      formData.description = row.description
    } else {
      // 新增
      formData.id = ''
    }
    console.log('初始化')
  })
}

</script>

<style lang="scss" scoped></style>
